package com.lei.springbootpractice.config;

import com.lei.springbootpractice.pojo.ProductsPojo;
import com.lei.springbootpractice.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 性能测试控制器
 * 用于测试和监控数据库查询性能
 */
@RestController
@RequestMapping("/api/performance")
public class PerformanceTestController {

    @Autowired
    private ProductsService productsService;

    /**
     * 测试商品查询性能
     */
    @GetMapping("/test-product-query")
    public Map<String, Object> testProductQuery(
            @RequestParam(required = false) String categoryId,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice) {
        
        Map<String, Object> result = new HashMap<>();
        
        // 记录开始时间
        long startTime = System.currentTimeMillis();
        
        try {
            // 执行查询
            List<ProductsPojo> products = productsService.getProductsByFilter(categoryId, minPrice, maxPrice);
            
            // 记录结束时间
            long endTime = System.currentTimeMillis();
            long queryTime = endTime - startTime;
            
            result.put("success", true);
            result.put("queryTime", queryTime + "ms");
            result.put("productCount", products.size());
            result.put("message", "查询成功");
            
            // 如果查询时间超过1秒，提醒性能问题
            if (queryTime > 1000) {
                result.put("warning", "查询时间较长，建议检查索引和数据量");
            }
            
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            long queryTime = endTime - startTime;
            
            result.put("success", false);
            result.put("queryTime", queryTime + "ms");
            result.put("error", e.getMessage());
            result.put("message", "查询失败");
        }
        
        return result;
    }

    /**
     * 测试数据库连接性能
     */
    @GetMapping("/test-connection")
    public Map<String, Object> testConnection() {
        Map<String, Object> result = new HashMap<>();
        
        long startTime = System.currentTimeMillis();
        
        try {
            // 简单查询测试连接
            List<ProductsPojo> products = productsService.getAllProducts();
            
            long endTime = System.currentTimeMillis();
            long queryTime = endTime - startTime;
            
            result.put("success", true);
            result.put("connectionTime", queryTime + "ms");
            result.put("totalProducts", products.size());
            result.put("message", "数据库连接正常");
            
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            long queryTime = endTime - startTime;
            
            result.put("success", false);
            result.put("connectionTime", queryTime + "ms");
            result.put("error", e.getMessage());
            result.put("message", "数据库连接异常");
        }
        
        return result;
    }

    /**
     * 获取性能优化建议
     */
    @GetMapping("/optimization-tips")
    public Map<String, Object> getOptimizationTips() {
        Map<String, Object> result = new HashMap<>();
        
        result.put("tips", List.of(
            "1. 确保为商品表的category_id字段添加了索引",
            "2. 确保为商品表的price字段添加了索引", 
            "3. 避免查询过多数据，使用分页限制",
            "4. 定期清理无用数据",
            "5. 考虑使用缓存机制",
            "6. 监控SQL执行时间，优化慢查询"
        ));
        
        result.put("currentSettings", Map.of(
            "cacheSize", "8192",
            "lockTimeout", "10000ms",
            "connectionPoolSize", "5-20",
            "queryLimit", "1000条"
        ));
        
        return result;
    }
} 